package com.chanyachting.emall.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Admin;
import com.chanyachting.emall.form.AdminForm;
import com.chanyachting.emall.service.AdminService;
import com.chanyachting.emall.serviceImpl.AdminServiceImpl;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.RequestUtil;

public class AdminAction extends DispatchAction {


	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			AdminForm aform=(AdminForm)form;
			
			//表单验证成功
			if(aform.validate(mapping, request).isEmpty()){
				
				AdminService aservice=new AdminServiceImpl();
				
				Admin admin=new Admin();
				
				BeanUtils.copyProperties(admin, aform);
				
				admin.setLoginDate(DateUtil.getDate());
				admin.setLoginIP(request.getRemoteAddr());
				
				Admin loginAdmin=aservice.login(admin);
				
				//登录成功
				if(loginAdmin!=null){
					
					//将admin对象保存到session
					ru.setSessionObject("admin", loginAdmin);
					
					return mapping.findForward("loginsuccess");
					
				}else{
					
					//登录失败
					ru.setMsg("登录失败，请输入有效的登录账号，密码！");
				}
			}
			
		}
		
		//注销session
		ru.removeSessionObject("admin");
		
		return mapping.findForward("login");
	}
	
	
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		//注销session
		ru.removeSessionObject("admin");
		
		ru.setMsg("退出成功！");
		
		return mapping.findForward("login");
	}
	
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			AdminForm aform=(AdminForm)form;
			
			if(aform.validate(mapping, request).isEmpty()){
				
				Admin admin=new Admin();
				
				BeanUtils.copyProperties(admin, aform);
				
				if(admin.save()){
					
					ru.setSuccessMsg();
					
				}else ru.setFailMsg();
				
			}
			
		}
		
		
		return mapping.findForward("create");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			AdminForm aform=(AdminForm)form;
			
			if(aform.validate(mapping, request).isEmpty()){
				
				Admin admin=new Admin(id);
				
				BeanUtils.copyProperties(admin, aform);
				
				if(admin.update()){
					
					ru.setSuccessMsg();
					
				}else ru.setFailMsg();
				
			}
			
		}
		
		//重新加载admin对象
		ru.setAttribute("admin", new Admin(id));

		return mapping.findForward("update");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Admin admin=new Admin(id);
		
		if(admin.delete()){
			
			ru.setSuccessMsg();
		}else{
			
			ru.setFailMsg();
		}
		
		
		return mapping.findForward("delete");
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("adminList", Admin.list());
		
		return mapping.findForward("list");
	}
	

}
